import sys
import asyncio
import time
from loguru import logger


def init_log():
    logger.remove(handler_id=None)
    log_format = "{time:YYYY.MM.DD-HH.mm.ss.SSS} {message}"
    logger.add(sys.stderr, format=log_format)


async def test_a(sleep_time, what):
    while True:
        logger.debug(f"start {what}")
        await asyncio.sleep(sleep_time)
        logger.debug(f"end {what}")


async def test_b(sleep_time, what):
    while True:
        logger.debug(f"start {what}")
        await asyncio.sleep(sleep_time)
        logger.debug(f"end {what}")


async def main():
    print(f"started at {time.strftime('%X')}")

    await test_a(1, "hello")
    await test_b(2, "world")

    print(f"finished at {time.strftime('%X')}")


async def main_v2():
    task1 = asyncio.create_task(test_a(2, "hello"))
    task2 = asyncio.create_task(test_b(5, "world"))
    # await task1,task2
    await task1
    await task2


asyncio.run(main())
asyncio.run(main_v2())

""" RUN
asyncio.create_task() 可以让两个协程, 共同运行


"""
